﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace MyImaginaryFriends_Redux.Screens
{
    /// <summary>
    /// This class is the main title screen for the game.
    /// It will display all menu navigation options for the player at the title, as well as any other cool
    /// stuff we'd add to the background (like animated backgrounds and such)
    /// </summary>
    /// <remarks>
    /// the Menu Content Folder will hold ALL menu content for ALL menus. That includes in-game Menus.
    /// </remarks>

   

    class MainMenu
    {
        
        /// <summary>
        /// All Variables will be private and ONLY changeable and Visible through the Get-Set Functions, and their own respective classes.
        /// </summary>
        #region Variables

        private State MainMenuState;

        private Song bgm_MainMenu;

        private SoundEffect se_MenuScroll;
        private SoundEffect se_MenuSelect;

        private SoundEffectInstance sei_MenuScroll;
        private SoundEffectInstance sei_MenuSelect;

        private Texture2D t2d_Background;
        private SpriteFont sf_TitleScreenFont;

        private string s_StartGame, s_Options, s_Exit;

        private Color c_Selected, c_Unselected;

        private float f_SelectedScale, f_UnselectedScale;

        private int i_Selected;

        private bool b_cmd;//temporary exit command.

        #endregion


        /// <summary>
        /// Constructor for the main menu title screen
        /// </summary>
        /// <remarks>
        /// Unlike last time, each class will contain a LOAD function separate from the constructor.
        /// It caused too much confusion.
        /// </remarks>
        public MainMenu()
        {
            //set any values that would need to be set here; as well as initialize the class.

            MainMenuState = State.Active;//set this to active upon first load because it's the title screen.

            s_StartGame = "Start Game";
            s_Options = "Options";
            s_Exit = "Exit";

            c_Selected = Color.Red;
            c_Unselected = Color.White;

            f_SelectedScale = 2.0f;
            f_UnselectedScale = 1.0f;

            i_Selected = 0;

        }

        //I Know there's an eaiser way to do this, SAM. I just want this for my own health, OKAY?!
        public State GetMainMenuState()
        {
            return MainMenuState;
        }

        public void SetMainMenuState(State In_State)
        {
            MainMenuState = In_State;
        }
        //?!

        //I think we'd be better off having one content manager pass all content to the loadcontent
        //functions of each class. we can try the other way after we get the game finished, to see which is more efficient
        public void LoadContent(ContentManager content)
        {
            t2d_Background = content.Load<Texture2D>("MenuContent/MainMenuBG");
            sf_TitleScreenFont = content.Load<SpriteFont>("MenuContent/Arial");

            bgm_MainMenu = content.Load<Song>("Audio/Music/DarkestChild");

            se_MenuScroll = content.Load<SoundEffect>("Audio/SoundEffects/MenuScroll");
            se_MenuSelect = content.Load<SoundEffect>("Audio/SoundEffects/MenuSelect");

            sei_MenuScroll = se_MenuScroll.CreateInstance();
            sei_MenuSelect = se_MenuSelect.CreateInstance();
        }

        //I will make the input handler a class in the MainType.cs and pass it down all update functions.
        //I will also pass all screens to all other screens... as stupid as that sounds. you'll see why.
        public void Update(GameTime gameTime, InputHandler input, Screens.TitleOptionsMenu optionsScreen, Screens.GameplayScreen GameScreen)
        {
            //this will handle the selection process for the options.
            if (MainMenuState == State.Active)
            {
                if (MediaPlayer.State != MediaState.Playing)
                {
                    MediaPlayer.Play(bgm_MainMenu);
                }

                switch (i_Selected)
                {
                    case 0:
                        //no selection is covered.
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 1;//move to select start game
                        }
                        break;

                    case 1: //Start Game is Selected
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 2;//move to select options
                        }
                        
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            sei_MenuSelect.Play();

                            if (GameScreen.GetGamePlayScreenState() == State.Hidden)
                            {
                                GameScreen.SetGamePlayScreenState(State.Loading);
                                GameScreen.b_fromNewGame = true;
                            }

                            MediaPlayer.Stop();
                            MainMenuState = State.Hidden;
                        }
                        break;

                    case 2://Options is Selected
                        if (input.GetKBStateSingle(Keys.Down) || input.GetGPStateSingle(Buttons.DPadDown))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 3;//move to select exit
                        }

                        if (input.GetKBStateSingle(Keys.Up) || input.GetGPStateSingle(Buttons.DPadUp))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 1;//move back to select Start Game
                        }
                         
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            sei_MenuSelect.Play();

                            if (optionsScreen.GetOptionsMenuState() == State.Hidden)
                                optionsScreen.SetOptionsMenuState(State.Active);

                            MainMenuState = State.Hidden;
                        }
                        break;

                    case 3://Exit is Selected
                        if (input.GetKBStateSingle(Keys.Up) || input.GetGPStateSingle(Buttons.DPadUp))
                        {
                            sei_MenuScroll.Play();
                            i_Selected = 2;//move back to select Options
                        }
                         
                        if (input.GetKBStateSingle(Keys.Enter) || input.GetGPStateSingle(Buttons.A))
                        {
                            b_cmd = true;
                        }
                        break;

                }//end switch
            }//end if
        }

        public bool PassExitCommand()
        {
            return b_cmd;
        }

        public void Draw(SpriteBatch spriteBatch, Screens.TitleOptionsMenu optionsScreen)
        {
            if (MainMenuState == State.Active)
            {
                spriteBatch.Begin();

                spriteBatch.Draw(t2d_Background, new Vector2(spriteBatch.GraphicsDevice.Viewport.Width / 2, spriteBatch.GraphicsDevice.Viewport.Height / 2), null, Color.White, 0.0f, new Vector2(t2d_Background.Width / 2, t2d_Background.Height / 2), 1.0f, SpriteEffects.None, 1.0f);
                //I will definitely clean ^ this up. make it a single variable for CenterOfScreen and OriginOfSprite

                spriteBatch.DrawString(sf_TitleScreenFont,"My Imaginary Friends", new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) -200, 50), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);

                switch (i_Selected)
                {
                    case 1:
                        spriteBatch.DrawString(sf_TitleScreenFont, s_StartGame, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 150), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Options, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 250), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Exit, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 350), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;

                    case 2:
                        spriteBatch.DrawString(sf_TitleScreenFont, s_StartGame, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 150), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Options, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 250), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Exit, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 350), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;

                    case 3:
                        spriteBatch.DrawString(sf_TitleScreenFont, s_StartGame, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 150), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Options, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 250), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Exit, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 350), c_Selected, 0.0f, Vector2.Zero, f_SelectedScale, SpriteEffects.None, 1.0f);
                        break;

                    default:
                        spriteBatch.DrawString(sf_TitleScreenFont, s_StartGame, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 150), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Options, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 250), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        spriteBatch.DrawString(sf_TitleScreenFont, s_Exit, new Vector2((spriteBatch.GraphicsDevice.Viewport.Width / 2) - 100, 350), c_Unselected, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                        break;
                }

                //spriteBatch.DrawString(sf_TitleScreenFont, "OP:"+optionsScreen.GetOptionsMenuState().ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                //spriteBatch.DrawString(sf_TitleScreenFont, "TI:"+MainMenuState.ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y + 20), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);
                //spriteBatch.DrawString(sf_TitleScreenFont, i_Selected.ToString(), new Vector2(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y + 40), c_Unselected * 0.5f, 0.0f, Vector2.Zero, f_UnselectedScale, SpriteEffects.None, 1.0f);

                spriteBatch.End();
            }
        }

    }
}
